package com.horay.springclouddemo.consumer.hystrix.controller;

import com.horay.springclouddemo.common.http.Responses;
import com.horay.springclouddemo.common.http.ResultBody;
import com.horay.springclouddemo.consumer.hystrix.client.HystrixFeignClient;
import com.horay.springclouddemo.model.hystrix.Hystrix;
import com.horay.springclouddemo.model.hystrix.HystrixDTO;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 豪猪控制器
 *
 * @author heyingcheng
 * @date 2020/3/21 13:52
 */
@Slf4j
@DefaultProperties(defaultFallback = "defaultFallbackMethod")
@RestController
@RequestMapping("consumer/hystrix")
public class HystrixController {

    private final HystrixFeignClient hystrixFeignClient;

    public HystrixController(@Qualifier("HystrixFeignClient") HystrixFeignClient hystrixFeignClient) {
        this.hystrixFeignClient = hystrixFeignClient;
    }

    private ResponseEntity<ResultBody> defaultFallbackMethod() {
        return Responses.unprocessableEntityMessage("defaultFallbackMethod");
    }

    @HystrixCommand
    @PostMapping
    public ResponseEntity<ResultBody> createHystrix(@RequestBody Hystrix hystrix) {
        return hystrixFeignClient.createHystrix(hystrix);
    }

    @HystrixCommand(fallbackMethod = "getHystrixFallbackMethod", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
    })
    @GetMapping("{id}")
    public ResponseEntity<ResultBody> getHystrix(@PathVariable Integer id) {
        return hystrixFeignClient.getHystrix(id);
    }

    private ResponseEntity<ResultBody> getHystrixFallbackMethod(@PathVariable Integer id) {
        return Responses.unprocessableEntity();
    }

    @HystrixCommand
    @GetMapping("hystrixs")
    public ResponseEntity<ResultBody> getHystrixs(@RequestBody HystrixDTO hystrixDTO) {
        return hystrixFeignClient.getHystrixs(hystrixDTO);
    }

    @HystrixCommand
    @PutMapping
    public ResponseEntity<ResultBody> updateHystrix(Hystrix hystrix) {
        return hystrixFeignClient.updateHystrix(hystrix);
    }

    @DeleteMapping("{id}")
    public ResponseEntity<ResultBody> deleteHystrix(@PathVariable Integer id) {
        return hystrixFeignClient.deleteHystrix(id);
    }

}
